package com.atguigu.controller;


import com.atguigu.base.BaseController;
import com.atguigu.entity.*;
import com.atguigu.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/house")
public class HouseController extends BaseController {
    @DubboReference
    private HouseService houseService;

    @DubboReference
    private DictService dictService;

    @DubboReference
    private CommunityService communityService;

    @DubboReference
    private HouseImageService houseImageService;

    @DubboReference
    private HouseBrokerService houseBrokerService;

    @DubboReference
    private HouseUserService houseUserService;

    @RequestMapping
    public String index(HttpServletRequest request, Model model) {
        Map<String, Object> filters = getFilters(request);
//       根据条件查询分页信息
        PageInfo<House> page = houseService.findPage(filters);
        model.addAttribute("page", page);
//      筛选条件放入请求域
        model.addAttribute("filters", filters);

        model.addAttribute("communityList",communityService.findAll());
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/index";
    }

    /**
     * 进入新增
     * @param model
     * @return
     */
    @GetMapping("/create")
    public String create(ModelMap model) {
        model.addAttribute("communityList",communityService.findAll());
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/create";
    }

    /**
     * 保存新增
     * @param house
     * @return
     */
    @RequestMapping("/save")
    public String save(House house) {
        houseService.insert(house);
        return "common/successPage";
    }

    @GetMapping("/edit/{id}")
    public String edit(ModelMap model,@PathVariable Long id) {
        House house = houseService.getById(id);
        model.addAttribute("house",house);
        model.addAttribute("communityList",communityService.findAll());
        model.addAttribute("houseTypeList",dictService.findListByDictCode("houseType"));
        model.addAttribute("floorList",dictService.findListByDictCode("floor"));
        model.addAttribute("buildStructureList",dictService.findListByDictCode("buildStructure"));
        model.addAttribute("directionList",dictService.findListByDictCode("direction"));
        model.addAttribute("decorationList",dictService.findListByDictCode("decoration"));
        model.addAttribute("houseUseList",dictService.findListByDictCode("houseUse"));
        return "house/edit";
    }

    /**
     * 保存更新
     * @param house
     * @return
     */
    @PostMapping("/update")
    public String update(House house) {
        houseService.update(house);
        return "common/successPage";
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        houseService.delete(id);
        return "redirect:/house";
    }

    /**
     * 发布
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/publish/{id}/{status}")
    public String publish(@PathVariable Long id,@PathVariable Integer status) {
        houseService.publish(id, status);
        return "redirect:/house";
    }

    //    展示房源详情页
    @RequestMapping("/{id}")
    public String show(@PathVariable("id") long id,Map map) {
//        查询各种信息放入请求域
        House house = houseService.getById(id);
        map.put("house", house);
        Community community = communityService.getById(house.getCommunityId());
        map.put("community", community);
//        查询房产图片和房源图片
        List<HouseImage> houseImage1List = houseImageService.findList(house.getId(), 1);
        List<HouseImage> houseImage2List = houseImageService.findList(house.getId(), 2);
        map.put("houseImage1List", houseImage1List);
        map.put("houseImage2List", houseImage2List);
//        查询房产经纪人信息
        List<HouseBroker> houseBrokerList = houseBrokerService.findListByHouseId(id);
        map.put("houseBrokerList", houseBrokerList);
//        查询业主信息
        List<HouseUser> houseUserList = houseUserService.findListByHouseId(id);
        map.put("houseUserList", houseUserList);
//        返回show视图
        return "house/show";
    }
}
